//
//  QFeedBackVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/8/27.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QFeedBackVC.h"

@interface QFeedBackVC ()<UITextViewDelegate>

@property(nonatomic,strong)UITextView *textView;

@property(nonatomic,strong)UILabel *placeLabel;

@end

@implementation QFeedBackVC

- (id)init{
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupForDismissKeyboard];
    self.title = @"意见反馈";
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createUI];
}

- (void)createUI{
    _textView = [[UITextView alloc] initWithFrame:CGRectMake(0, kNavHeight+kStatusHeight, kScreenWidth, 200)];
    _textView.backgroundColor = [UIColor whiteColor];
    _textView.delegate = self;
    _textView.font = [UIFont systemFontOfSize:Ksize17];
    [self.view addSubview:_textView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [_textView addGestureRecognizer:tap];
    
    
    UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, kScreenWidth,44)];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 60, 7,50, 30)];
    [button setTitle:@"完成"forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(handleFinishBtn) forControlEvents:UIControlEventTouchUpInside];
    //
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    UIBarButtonItem *flexibleitem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:self action:nil];
    NSArray *items = @[flexibleitem,flexibleitem,item];
    [bar setItems:items animated:YES];
    
    _textView.inputAccessoryView = bar;
    
    _placeLabel = [QViewUtility createLabelWithFrame:CGRectMake(10,10, kScreenWidth, 20) font:Ksize16 textAlignment:NSTextAlignmentLeft textColor:[QColorUtility defaultLowGrayColor] text:@"请输入您的反馈意见"];
    [_textView addSubview:_placeLabel];
    
    UIButton *subButton = [QViewUtility createButtonWithFrame:CGRectMake(0, kScreenHeight-50, kScreenWidth, 50) font:17 title:@"提交" titleColor:[UIColor whiteColor] backgroundColor:[QColorUtility defaultBlueColor]];
    [subButton blueStyle];
    [subButton addTarget:self action:@selector(actionAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:subButton];
}
- (void)handleFinishBtn{
    
    [_textView resignFirstResponder];
}
- (void)actionAction{
    CLog(@"提交");
    if(_textView.text.length == 0){
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请输入您的反馈意见" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
    }else{
        [self requestData];
    }
}

- (void)tap{
    BOOL s = [_textView resignFirstResponder];
    if(s){
        [_textView resignFirstResponder];
    }else{
        [_textView becomeFirstResponder];
    }
}

#pragma mark --- textView delegate
- (void)textViewDidChange:(UITextView *)textView {
    CLog(@"textview====%@",textView.text);
    if(textView.text.length == 0){
        _placeLabel.hidden = NO;
    }else{
        _placeLabel.hidden = YES;
    }
}

- (void)requestData{
    NSString *url = [QParamService urlWithAction:@"customerFeedbackInformation"];
    NSDictionary *param = [QParamService feedBackString:_textView.text];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"提交成功" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

@end
